package cn.appsys.controller.appinfo;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import cn.appsys.pojo.BackendUser;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.service.app_category.AppCategoryService;
import cn.appsys.service.appversion.VersionService;
import cn.appsys.service.datadictionary.DataDictionaryService;
import cn.appsys.service.devuser.AppInfoService;
import cn.appsys.tools.Constants;
import cn.appsys.tools.PageSupport;

/**
 * 后台app管理控制器
 * 
 * @author 寇超强
 *
 */
@Controller
@RequestMapping(value = "sys/backend/app")
public class AppController {

	@Resource
	private AppInfoService appInfoService; // app信息service
	@Resource
	private DataDictionaryService dataDictionaryService; // 数据字典
	@Resource
	private AppCategoryService appCateService; // app分类
	@Resource
	private VersionService versionService; // 版本信息
	private List<AppInfo> appInfoList = null; // app数据集合

	/**
	 * 进入后台app信息查看 绑定所属平台
	 */
	@RequestMapping("/showAppInfo")
	public String showAppInfo(Model model,
			@RequestParam(value = "querySoftwareName", required = false) String querySoftwareName,
			@RequestParam(value = "queryCategoryLevel1", required = false) Integer queryCategoryLevel1,
			@RequestParam(value = "queryCategoryLevel2", required = false) Integer queryCategoryLevel2,
			@RequestParam(value = "queryCategoryLevel3", required = false) Integer queryCategoryLevel3,
			@RequestParam(value = "queryFlatformId", required = false) Integer queryFlatformId,
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex) {
		// app信息列表

		// 数据字典表获取所属平台
		List<DataDictionary> statusList = null;
		// 列出一级分类列表
		List<AppCategory> categroyLevel1List = null;
		List<AppCategory> categroyLevel2List = null;
		List<AppCategory> categroyLevel3List = null;
		PageSupport pages = null;
		// 绑定app所属平台
		try {
			statusList = dataDictionaryService.getDataDictionaryList("APP_FLATFORM");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 查询一级分类 列表
		categroyLevel1List = appCateService.getAppCategory(0);

		try {
			Integer currentPageNo = 1;
			if (pageIndex != null) {
				currentPageNo = Integer.valueOf(pageIndex);
			}
			// 总数量(列表)
			int totalCount = 0;

			try {
				totalCount = appInfoService.getAppInfoCount(querySoftwareName, 1, queryCategoryLevel1,
						queryCategoryLevel2, queryCategoryLevel3, queryFlatformId, null);
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 总页数
			pages = new PageSupport();
			pages.setCurrentPageNo(currentPageNo);
			pages.setPageSize(Constants.pageSize);
			pages.setTotalCount(totalCount);
			int totalPageCount = pages.getTotalPageCount();
			// 控制首页和尾页
			if (currentPageNo < 1) {
				currentPageNo = 1;
			} else if (currentPageNo > totalPageCount) {
				currentPageNo = totalPageCount;
			}
			appInfoList = appInfoService.getAppInfoList(querySoftwareName, 1, queryCategoryLevel1, queryCategoryLevel2,
					queryCategoryLevel3, queryFlatformId, null, currentPageNo, Constants.pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("appInfoList", appInfoList);
		model.addAttribute("statusList", statusList);
		model.addAttribute("categoryLevel1List", categroyLevel1List);
		model.addAttribute("pages", pages);
		// model.addAttribute("queryStatus", queryStatus);
		model.addAttribute("querySoftwareName", querySoftwareName);
		model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
		model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
		model.addAttribute("queryCategoryLevel3", queryCategoryLevel3);
		model.addAttribute("queryFlatformId", queryFlatformId);
		// 二级分类列表和三级分类列表---回显
		categroyLevel2List = appCateService.getAppCategory(queryCategoryLevel1);
		model.addAttribute("categoryLevel2List", categroyLevel2List);
		categroyLevel3List = appCateService.getAppCategory(queryCategoryLevel2);
		model.addAttribute("categoryLevel3List", categroyLevel3List);
		return "backend/appinfolist";
	}

	/**
	 * 查询二三级非分类
	 */
	@RequestMapping(value = "/categorylevellist", produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public Object categorylevellist(@RequestParam("pid") int pid) {
		System.out.println(pid);
		List<AppCategory> categroyLevel2List = appCateService.getAppCategory(pid);
		return JSON.toJSONString(categroyLevel2List);
	}

	/**
	 * 根据appinfoid获取app信息 存放到model对象中
	 */
	public void getAppInfo(Integer appId, Model model) {
		for (AppInfo appInfo : appInfoList) {
			if (appInfo.getId() == appId)
				model.addAttribute("appInfo", appInfo);
		}
	}

	/**
	 * app信息查看审核
	 */
	@RequestMapping(value = "/check")
	public String checkApp(Integer aid, Integer vid, Model model) {
		getAppInfo(aid, model);
		List<AppVersion> versions = versionService.getAppVersion(aid);
		AppVersion version = null;
		if (versions.size() == 1)
			version = versions.get(0);
		else if (versions.size() > 1) {
			version = versions.get(0);
			for (int i = 0; i < versions.size() - 1; i++) {
				if (versions.get(i).getCreationDate().getTime() < versions.get(i + 1).getCreationDate().getTime())
					version = versions.get(i + 1);
			}
		}
		model.addAttribute("appVersion", version);
		return "backend/appcheck";
	}

	/**
	 * app审核
	 */
	@RequestMapping(value = "/checksave")
	public String checksave(Integer id, Integer status, HttpSession session) {
		if (appInfoService.updateAppStatus(id, status,
				((BackendUser) session.getAttribute(Constants.USER_SESSION)).getId(), new Date()) > 0)
			return "redirect:/sys/backend/app/showAppInfo";
		else
			return "checksave";
	}

}
